package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.goods

import android.content.Context
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.library_common.adapter.JssBaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.bean.AreaItemBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.config.PROVINCE
import com.google.android.material.tabs.TabLayout
import com.lxj.xpopup.core.BottomPopupView

class SelectLocationNewPop(context: Context) : BottomPopupView(context),
    TabLayout.OnTabSelectedListener, OnItemClickListener {

    private var mTabLayout: TabLayout? = null

    private lateinit var mRecyclerView: RecyclerView

    private lateinit var mAdapter: JssBaseQuickAdapter<AreaItemBean>

    private lateinit var mProgressBar: ProgressBar

    private lateinit var sureTv: TextView

    private var resTv: TextView? = null

    private val mData = mutableListOf<AreaItemBean>()

    private var onSelectListener: SelectLocationPop.OnSelectListener? = null

    private var selectedItem: AreaItemBean? = null

    fun setOnSelectListener(onSelectListener: SelectLocationPop.OnSelectListener): SelectLocationNewPop {
        this.onSelectListener = onSelectListener
        return this
    }

    override fun getPopupHeight(): Int {
        return PixUtils.dp2px(380)
    }

    override fun getImplLayoutId(): Int {
        return R.layout.select_location_new_pop
    }

    override fun onCreate() {
        super.onCreate()
        mTabLayout = findViewById(R.id.mTabLayout)
        mProgressBar = findViewById(R.id.mProgressBar)
        sureTv = findViewById(R.id.sureTv)
        resTv = findViewById(R.id.resTv)

        sureTv.setOnClickListener {
            val addresses = addresses()
            if (addresses.isEmpty()) {
                Toast.makeText(context, "请选择具体地址", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            onSelectListener?.onSelected(addresses)

            dismiss()

        }

        mRecyclerView = findViewById(R.id.mRecyclerView)
        mRecyclerView.setHasFixedSize(true)
        mRecyclerView.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mAdapter =
            object : JssBaseQuickAdapter<AreaItemBean>(R.layout.select_location_item_pop, mData) {

                override fun convert(holder: JssBaseViewHolder?, item: AreaItemBean?) {
                    super.convert(holder, item)
                    val textColor = if (selectedItem?.areaName == (item?.areaName
                            ?: "textColor")
                    ) R.color.PrimaryColor else R.color.blackAlpha55

                    holder?.setText(R.id.textTv, item?.areaName)
                        ?.setTextColorRes(R.id.textTv, textColor)
                }

            }
        mRecyclerView.adapter = mAdapter

        mProgressBar.visibility = VISIBLE

        mAdapter.setOnItemClickListener(this)

        mTabLayout?.addOnTabSelectedListener(this)

        getLocation()
    }

    private fun getLocation() {
        resTv?.visibility = GONE

        mProgressBar.visibility = VISIBLE

        sureTv.visibility = GONE

        var areaTypeStr = if (selectedItem == null)
            "$PROVINCE"
        else
            selectedItem?.areaType.toString()



    }

    private fun addresses(): List<AreaItemBean?> {
//        return getAreaName(selectedItem)

        val ls = mutableListOf<AreaItemBean?>()
        getAreaList(selectedItem, ls)
        ls.reverse()
        return ls
    }

    private fun getAreaList(
        selectedItem: AreaItemBean?,
        ls: MutableList<AreaItemBean?>
    ): List<AreaItemBean?> {
        if (selectedItem != null) {
            ls.add(selectedItem)
        }

        if (selectedItem?.parentArea != null) {
            getAreaList(selectedItem.parentArea, ls)
        }

        return ls
    }

    override fun dismiss() {
        super.dismiss()
        onSelectListener = null
    }

    override fun onDismiss() {
        super.onDismiss()
        mTabLayout?.removeOnTabSelectedListener(this)
    }

    interface OnSelectListener {
        fun onSelected(ls: List<AreaItemBean?>)
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {

    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {

    }

    override fun onTabReselected(tab: TabLayout.Tab?) {

    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

    }
}